//
//  SettingViewController.m
//  QuickCare
//
//  Created by lidabiao on 16/7/14.
//  Copyright © 2016年 lidabiao. All rights reserved.
//

#import "SettingViewController.h"
#import "DrawerManager.h"
#import "CoreDataManager.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "ReviseViewController.h"
#import "ExplainViewController.h"
#import "AdviceViewController.h"

static NSString *reuseIdentifier = @"reuseIdentifier";

@interface SettingViewController ()

@property (nonatomic, strong) NSArray *arr;

@end

@implementation SettingViewController

-(NSArray *)arr
{
    if (_arr == nil)
    {
        _arr = @[@"意见反馈", @"说明", @"修改密码", @"注销用户"];
    }
    return _arr;
}

- (void)viewDidLoad {
    [super viewDidLoad];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: reuseIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    
    cell.textLabel.text = self.arr[indexPath.row];
    
    cell.textLabel.textColor = [UIColor darkGrayColor];
    
    cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"position-right"]];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        AdviceViewController *adviceVC = [[AdviceViewController alloc] init];
        
        [self.navigationController pushViewController:adviceVC animated:YES];
    }
    if (indexPath.row == 1)
    {
        ExplainViewController *explainVC = [[ExplainViewController alloc] init];
        
        [self.navigationController pushViewController:explainVC animated:YES];
    }
    if (indexPath.row == 2)
    {
        UIStoryboard *st = [UIStoryboard storyboardWithName:@"Revise" bundle:nil];
        
        ReviseViewController *rewiseVC = [st instantiateInitialViewController];
        
        rewiseVC.title = self.arr[indexPath.row];
        
        rewiseVC.userInfo = self.userInfo;
        
        [self.navigationController pushViewController:rewiseVC animated:YES];
    }
    if (indexPath.row == 3)
    {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您确定要注销登录吗？" preferredStyle: UIAlertControllerStyleActionSheet];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action)
        {
            //将数据库中的用户登录状态改为NO
            self.userInfo.isLogin = 0;
            
            [kCoreDataManager save];
            
            [kDrawerManager enableLeftDrawerAndOpenLeftDrawer];
            
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

        [alert addAction:action1];
        
        [alert addAction:action2];
        
        [self presentViewController:alert animated:YES completion:nil];
    }
}

@end
